package com.hyl.crmclient.filter;


import javax.annotation.Resource;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Description TODO
 * @Author hyl
 * @Date 2022/5/31 15:36
 **/
public class LoginValidateFilter implements Filter {


	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		System.out.println("过滤器 前置");

	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
			throws IOException, ServletException {
		System.out.println("过滤器 处理。。");
		//判断用户是否登入==》session中是否有登入用户的信息
//		HttpServletRequest request = (HttpServletRequest) servletRequest;
//		HttpServletResponse response  = (HttpServletResponse) servletResponse;
//		if(request.getSession().getAttribute("loginuser") == null){ //用户未登入
//			response.setCharacterEncoding("utf-8");
//			response.setContentType("text/html;charset=UTF-8");
//			PrintWriter out = response.getWriter();
//			out.print("<script>" +
//					"alert('请登入');" +
//					"location.href='jsp/login.jsp';" +
//					"</script>");
//			out.flush();
//			out.close();;
//		}else{//已登入
//			//进入用户实际请求的==》controller
//			filterChain.doFilter(servletRequest,servletResponse);
//		}

		filterChain.doFilter(servletRequest,servletResponse);
	}

	@Override
	public void destroy() {
		System.out.println("过滤器 后置");
	}
}
